﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.Security;

namespace CarBlackBox
{
    public partial class Form_UserEditor : Form
    {
        MembershipUser user = null;

        public MembershipUser User
        {
            get { return user; }
            set { user = value; }
        }
        string parent;
        bool bClose = true;
        public Form_UserEditor(string P)
        {
            InitializeComponent();
            parent = P;
            foreach (string role in Roles.GetRolesForUser(parent) )
            {
                checkedListBox1.Items.Add(role, true);
            }
        }
        public Form_UserEditor(string P,string username)
        {
            InitializeComponent();
            parent = P;
            foreach (string role in Roles.GetRolesForUser(parent))
            {
                checkedListBox1.Items.Add(role, true);
            }
            user = Membership.GetUser(username);
        }
        private void Form_UserEditor_Load(object sender, EventArgs e)
        {
            if (user == null)//添加
            {
                this.Text = this.Text + "添加用户";
                btResetPassword.Visible = false;
            }
            else//更改
            {
                this.Text = this.Text + "修改" + user.UserName + "信息";
                tbName.Text = user.Comment;
                tbUserName.Text = user.UserName;
                tbPassword.Visible = false;
                tbPassword2.Visible = false;
                tbUserName.ReadOnly = true;
                if (user.IsLockedOut)
                    button3.Visible = true;
                button4.Visible = true;
                if (user.IsApproved)
                {
                    button4.Text = "挂起用户";
                }
                else
                    button4.Text = "使能用户";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (WFNetLib.WFGlobal.MessageYseOrNo("确定取消当前操作么？", "用户管理") == DialogResult.No)
                bClose = false;
        }

        private void Form_UserEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!bClose)
            {
                e.Cancel = true;
                bClose = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkedListBox1.CheckedItems.Count == 0)
            {
                MessageBox.Show("用户权限不能为空");
                return;
            }
            if (user == null)//新建
            {
                if (!WFNetLib.Strings.StringsFunction.IsValiable_UserName(tbUserName.Text))
                {
                    MessageBox.Show("用户名只能由数字、字母或下划线构成，长度为4~16，当前输入不符号规范，请重新输入");
                    tbUserName.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(tbName.Text))
                {
                    MessageBox.Show("用户名不能为空");
                    tbName.Focus();
                    return;
                }
                if (!string.Equals(tbPassword.Text, tbPassword2.Text) || string.IsNullOrEmpty(tbPassword.Text) || tbPassword.Text.Length < 5)
                {
                    MessageBox.Show("两次输入的密码不相同且密码不能为空,密码长度不能小于5，请重新输入");
                    tbPassword.Text = tbPassword2.Text = "";
                    return;
                }
                user = Membership.CreateUser(tbUserName.Text,tbPassword.Text);
                user.Comment = tbUserName.Text;
                Membership.UpdateUser(user);
                WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                log.LogType = LogType.用户管理.ToString();
                log.LogContent = "添加用户名为" + tbUserName.Text + "的用户";
                log.LogRemark = "用户添加";
                log.InserSQLLog();
            }
            else
            {
                user.Comment = tbUserName.Text;
                Membership.UpdateUser(user); 
                Roles.RemoveUserFromRoles(user.UserName, Roles.GetRolesForUser(parent));
                WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                log.LogType = LogType.用户管理.ToString();
                log.LogContent = "修改用户名为" + user.UserName + "的用户";
                log.LogRemark = "用户修改";
                log.InserSQLLog();
            }
            foreach (object role in checkedListBox1.CheckedItems)
            {
                Roles.AddUserToRole(user.UserName, role.ToString());
            }
            MessageBox.Show("操作成功");
            this.DialogResult = DialogResult.OK;
        }

        private void btResetPassword_Click(object sender, EventArgs e)
        {
            MembershipUser user = Membership.GetUser(tbUserName.Text);
            string pwd = user.ResetPassword();
            user.ChangePassword(pwd, "888888");
            MessageBox.Show("用户：" + tbUserName.Text + " 密码重置为888888");
            WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
            log.LogType = LogType.用户管理.ToString();
            log.LogContent = "密码重置用户名为" + user.UserName + "的用户";
            log.LogRemark = "密码重置";
            log.InserSQLLog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            user.IsApproved = !user.IsApproved;
            Membership.UpdateUser(user);
            MessageBox.Show("操作成功");
            this.DialogResult = DialogResult.OK;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            user.UnlockUser();
            MessageBox.Show("操作成功");
            this.DialogResult = DialogResult.OK;
        }
    }
}
